package com.netcracker.educenter.filestorage.configurator;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;

public class Configurator {

    private static final Logger log = Logger.getLogger(Configurator.class);
    private static Configurator ourInstance = new Configurator();

    private String url;
    private String user;
    private String password;
    private String driver;
    private String path;
    private String registrationPageUrl;
    private String fileslistPageUrl;
    private String loginPageUrl;

    public String getRegistrationPageUrl() {
        return registrationPageUrl;
    }

    public String getFileslistPageUrl() {
        return fileslistPageUrl;
    }

    public String getLoginPageUrl() {
        return loginPageUrl;
    }

    public String getUrl() {
        return url;
    }

    public String getUser() {
        return user;
    }

    public String getPassword() {
        return password;
    }

    public String getDriver() {
        return driver;
    }

    public String getPath() {
        return path;
    }

    public static Configurator getConfigurator() {
        return ourInstance;
    }

    private Configurator() {

        try {
            Document document = getDocument();

            setJDBCConfigurations(document);
            setUploadConfigurations(document);
            setPagesConfigurations(document);

        } catch (Exception e) {
            log.error("Cannot get configuration", e);
        }
    }

    private void setUploadConfigurations(Document document) {

        NodeList nodeList = document.getElementsByTagName("upload");
        Element element = (Element) nodeList.item(0);

        path = element.getElementsByTagName("path").item(0).getTextContent();
    }

    private void setJDBCConfigurations(Document document) {

        NodeList nodeList = document.getElementsByTagName("jdbc");
        Element element = (Element) nodeList.item(0);

        url = element.getElementsByTagName("url").item(0).getTextContent();
        user = element.getElementsByTagName("user").item(0).getTextContent();
        password = element.getElementsByTagName("password").item(0).getTextContent();
        driver = element.getElementsByTagName("driver").item(0).getTextContent();
    }

    private void setPagesConfigurations(Document document) {

        NodeList nodeList = document.getElementsByTagName("page");
        for (int i = 0; i < nodeList.getLength(); i++){

            Element element = (Element)nodeList.item(i);
            String name = element.getElementsByTagName("name").item(0).getTextContent();
            String pageUrl = element.getElementsByTagName("url").item(0).getTextContent();

            switch (name) {
                case "registration":
                    registrationPageUrl = pageUrl;
                    break;
                case "fileslist":
                    fileslistPageUrl = pageUrl;
                    break;
                case "login":
                    loginPageUrl = pageUrl;
                    break;
            }
        }
    }

    private Document getDocument() throws ParserConfigurationException, IOException, SAXException {

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();

        Document document = builder.parse(getClass().getResourceAsStream("/configurations.xml"));
        document.getDocumentElement().normalize();

        return document;
    }
}
